package com.irs.irsforum.service.impl;

import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONArray;
import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.irs.irsforum.entity.Comment;
import com.irs.irsforum.entity.User;
import com.irs.irsforum.mapper.CommentMapper;
import com.irs.irsforum.mapper.PostMapper;
import com.irs.irsforum.mapper.UserMapper;
import com.irs.irsforum.service.ICommentService;
import com.irs.irsforum.utils.TimeUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yihuili
 * @since 2020-01-18
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    private static final Logger logger =
            LoggerFactory.getLogger(CommentServiceImpl.class);

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    PostMapper postMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer setComment(Comment comment) throws SQLException {
        Integer isExist = postMapper.hasPost(comment.getPostId());
        if (isExist == null || isExist == 0) {
            logger.error("帖子不存在");
            return 0;
        }
        comment.setCreationTime(TimeUtil.getSqlTime());
        Integer result;
        try {
            result = commentMapper.setComment(comment);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new SQLException("插入异常");
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delComment(Long commentId, Long userId) {

        Integer delResult = commentMapper.delComment(new Comment(commentId, userId, 0));
        return delResult;
    }

    @Override
    public Integer reply2comment(Comment comment) {
        User user = userMapper.getUserById(comment.getUserId());
        if(user == null ){
            logger.error("评论用户为空");
            return 0;
        }
        String reply = commentMapper.getReply(comment.getId());
        JSONArray objects = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content", comment.getContent());
        jsonObject.put("creationTime", new Date().toString());
        jsonObject.put("nickName", user.getNickName());

        if (!StringUtils.isBlank(reply)) {
            objects = JSONArray.parseArray(reply);
        }
        objects.add(jsonObject);
        comment.setReply(objects.toString());
        Integer reply2comment = commentMapper.reply2comment(comment);
        return reply2comment;
    }
}
